home *** CD-ROM | disk | FTP | other *** search
/ Workbench Design / WB Collection.iso / workbench werkzeuge / memory & system tools / tinymeter / source / tinymeter_main / main.c < prev    next >
C/C++ Source or Header  |  1996-04-07  |  11KB  |  429 lines

  1. #include <libraries/commodities.h>
  2. #include <proto/SysInfo.h>
  3. #include <libraries/SysInfo.h>
  4. #include <libraries/ScreenNotify.h>
  5. #include <exec/memory.h>
  6. #include <dos/dos.h>
  7. #include <intuition/intuition.h>
  8. #include <intuition/classusr.h>
  9. #include <intuition/gadgetclass.h>
  10. #include <intuition/cghooks.h>
  11. #include <intuition/icclass.h>
  12. #include <intuition/classes.h>
  13. #include <utility/tagitem.h>
  14. #include "gaugeclass.h"
  15. #include "tinymeter.h"
  16.  
  17. /* default shit */
  18. #define Version "4.01"
  19.  
  20. #ifndef MAKE_ID
  21. #define MAKE_ID(a,b,c,d) ((ULONG) (a)<<24 | (ULONG) (b)<<16 | (ULONG) (c)<<8 | (ULONG) (d))
  22. #endif
  23.  
  24. int             __nocommandline =1;
  25. /* unsigned long   __stack         =8000;*/
  26.  
  27. struct Library          *RetinaBase;
  28. struct Library          *ShutdownBase;
  29. struct Library          *TimerBase;
  30. struct Library          *SysInfoBase;
  31. struct Library          *VMMBase;
  32. struct Library          *ScreenNotifyBase;
  33.  
  34. extern struct WBStartup *_WBBenchMsg;
  35. extern struct Library   *CxBase;
  36. extern struct Library   *DataTypesBase;
  37.  
  38. show(char *Message)
  39. {
  40.     struct EasyStruct easymsg =
  41.     {
  42.         sizeof(struct EasyStruct),
  43.         0,
  44.         "TinyMeter " Version " information",
  45.         Message,
  46.         "OK"
  47.     };
  48.     EasyRequest(NULL, &easymsg, NULL);
  49. }
  50.  
  51. int getNum(struct tm_gau_set *list)
  52. {
  53.     int num=1;
  54.     while(list=list->next)num++;
  55.     return(num);
  56. }
  57.  
  58. useDefault(struct tm_sys_set *set,struct tm_data *data)
  59. {
  60.     struct tm_gau_set   *many,
  61.                         *act,
  62.                         *old;
  63.  
  64.     struct tm_sys_set default_set=
  65.     {
  66.         "TM40",
  67.         1,
  68.  
  69.         10,
  70.         10,
  71.         450,
  72.         2,
  73.  
  74.         "Workbench",
  75.  
  76.         "XHelvetica.font",
  77.         11,
  78.  
  79.         bg_file,
  80.         "hd0:prefs/patterns/leather",
  81.         FALSE,0x55555555,0x55555555,0x55555555,
  82.  
  83.         5,
  84.         1,
  85.  
  86.         0,
  87.         TRUE,
  88.  
  89.         win_normal,
  90.         0,
  91.         4,
  92.         4,
  93.         8,
  94.         8,
  95.  
  96.         bd_standard,
  97.         FALSE,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,
  98.         FALSE,0x0,0x0,0x0,
  99.  
  100.         idle_own
  101.     };
  102.  
  103.     struct tm_gau_set g4=
  104.     {
  105.         typ_volume,
  106.         typ_histmeter,
  107.  
  108.         200,
  109.  
  110.         "Quill.font",
  111.         11,
  112.  
  113.         "ChipMem",
  114.         "HD1: %kd kb,%p%% free",
  115.         "HD1:",
  116.         ind_right,
  117.         TRUE,
  118.         TRUE,
  119.         TRUE,
  120.         TRUE,
  121.         FALSE,
  122.         FALSE,
  123.         TRUE,
  124.  
  125.         TRUE,2,0,0,
  126.         TRUE,2,0,0,
  127.         FALSE,0,0xFFFFFFFF,0,
  128.         FALSE,0,0,0xFFFFFFFF,
  129.         FALSE,0xFFFFFFFF,0,0,
  130.         TRUE,2,0,0,
  131.         TRUE,1,0,0,
  132.         TRUE,1,0,0,
  133.  
  134.         NULL
  135.     };
  136.  
  137.     struct tm_gau_set g3=
  138.     {
  139.         typ_idle,
  140.         typ_histmeter,
  141.  
  142.         200,
  143.  
  144.         "Quill.font",
  145.         11,
  146.  
  147.         "ChipMem",
  148.         "CPU %p%% free",
  149.         "",
  150.         ind_right,
  151.         TRUE,
  152.         TRUE,
  153.         TRUE,
  154.         TRUE,
  155.         FALSE,
  156.         FALSE,
  157.         TRUE,
  158.  
  159.         TRUE,2,0,0,
  160.         TRUE,2,0,0,
  161.         FALSE,0,0xFFFFFFFF,0,
  162.         FALSE,0,0,0xFFFFFFFF,
  163.         FALSE,0xFFFFFFFF,0,0,
  164.         TRUE,2,0,0,
  165.         TRUE,1,0,0,
  166.         TRUE,1,0,0,
  167.  
  168.         &g4
  169.     };
  170.  
  171.  
  172.     struct tm_gau_set g2=
  173.     {
  174.         typ_fast,
  175.         typ_gauge,
  176.  
  177.         20,
  178.  
  179.         "XHelvetica.font",
  180.         11,
  181.  
  182.         "FastMem",
  183.         "%td Bytes free",
  184.         "",
  185.         ind_right,
  186.         TRUE,
  187.         TRUE,
  188.         TRUE,
  189.         TRUE,
  190.         FALSE,
  191.         FALSE,
  192.         FALSE,
  193.  
  194.         TRUE,2,0,0,
  195.         TRUE,2,0,0,
  196.         FALSE,0,0xFFFFFFFF,0,
  197.         FALSE,0,0,0xFFFFFFFF,
  198.         FALSE,0xFFFFFFFF,0,0,
  199.         TRUE,2,0,0,
  200.         TRUE,1,0,0,
  201.         TRUE,0,0,0,
  202.  
  203.         &g3,
  204.     };
  205.  
  206.  
  207.     struct tm_gau_set g1=
  208.     {
  209.         typ_chip,
  210.         typ_gauge,
  211.  
  212.         20,
  213.  
  214.         "XHelvetica.font",
  215.         11,
  216.  
  217.         "ChipMem",
  218.         "%td Bytes free",
  219.         "",
  220.         ind_right,
  221.         TRUE,
  222.         TRUE,
  223.         TRUE,
  224.         TRUE,
  225.         FALSE,
  226.         FALSE,
  227.         FALSE,
  228.  
  229.         TRUE,2,0,0,
  230.         TRUE,2,0,0,
  231.         FALSE,0,0xFFFFFFFF,0,
  232.         FALSE,0,0,0xFFFFFFFF,
  233.         FALSE,0xFFFFFFFF,0,0,
  234.         TRUE,2,0,0,
  235.         TRUE,1,0,0,
  236.         TRUE,0,0,0,
  237.  
  238.         &g2
  239.     };
  240.  
  241.     CopyMem((char *)&default_set,set,sizeof(struct tm_sys_set));
  242.  
  243.     if(act=(struct tm_gau_set *)AllocVec(sizeof(struct tm_gau_set),0L))
  244.     {
  245.         many=&g1; data->list=act; CopyMem((char *)many,act,sizeof(struct tm_gau_set));
  246.         while(many=many->next)
  247.         {
  248.             old=act;
  249.             if(act=(struct tm_gau_set *)AllocVec(sizeof(struct tm_gau_set),0L))
  250.             {
  251.                 old->next=act;
  252.                 CopyMem((char *)many,act,sizeof(struct tm_gau_set));
  253.             }
  254.             else {act->next=0L;return;}
  255.         }
  256.         act->next=0L;
  257.     }
  258.     else data->list=0L;
  259. }
  260.  
  261.  
  262. int main(void)
  263. {
  264.  
  265.     struct tm_sys_set   *set;
  266.     struct tm_data      *data;
  267.  
  268.     Class               *gclass;    
  269.     ULONG                cxsigflag;
  270.     UBYTE               *my_file;
  271.     CxMsg               *msg;
  272.     CxObj               *broker;
  273.     struct MsgPort      *broker_mp;
  274.     static char         *version="$VER: TinyMeter V" Version;
  275.  
  276.     struct NewBroker newbroker =
  277.     {
  278.         NB_VERSION,
  279.         "TinyMeter V" Version,
  280.         "TinyMeter V" Version " © by Tinic Urou",
  281.         "Shows system states",
  282.         NBU_UNIQUE,
  283.         0,
  284.         -3,
  285.         0,
  286.         0
  287.     };
  288.  
  289.     if(set=(struct tm_sys_set *)AllocVec(sizeof(struct tm_sys_set),MEMF_CLEAR))
  290.     {
  291.         if(data=(struct tm_data *)AllocVec(sizeof(struct tm_data),MEMF_CLEAR))
  292.         {
  293.             if(gclass=(Class *)initGaugeGadgetClass())
  294.             {
  295.                 char  *my_file;
  296.                 struct tm_gau_set *many,*act;
  297.                 if(my_file=(UBYTE *)Open("ENV:TinyMeter",MODE_OLDFILE))
  298.                 {
  299.                     if(!Read(my_file,set,(ULONG)sizeof(struct tm_sys_set)))
  300.                     {
  301.                         show("Loading of preferences failed! Using defaults.");
  302.                         useDefault(set,data);
  303.                     }
  304.                     else
  305.                     {
  306.                         if((ULONG)*((ULONG *)&set->set_header[0])==0x544D3430)
  307.                         {
  308.                             act=(struct tm_gau_set *)AllocVec(sizeof(struct tm_gau_set),NULL);
  309.                             data->list=act; many=act;
  310.                             while(Read((char *)my_file,(char *)act,(ULONG)sizeof(struct tm_gau_set)))
  311.                             {
  312.                                 many=act;
  313.                                 act=(struct tm_gau_set *)AllocVec(sizeof(struct tm_gau_set),NULL);
  314.                                 many->next=act;
  315.                             }
  316.                             many->next=NULL; FreeVec(act); act=NULL;
  317.                         }
  318.                         else
  319.                         {
  320.                             show("Not a TinyMeter4.0 preferences file! Using defaults.");
  321.                             useDefault(set,data);
  322.                         }
  323.                     }
  324.                     Close(my_file);
  325.                 }
  326.                 else
  327.                 {
  328.                     show("No preferences file available! Using defaults.");
  329.                     useDefault(set,data);
  330.                 }
  331.                 data->num_of_gaug=getNum(data->list);
  332.  
  333.                 SetTaskPri(FindTask(0L),-3L);
  334.  
  335.                 Delay(set->start_wait*50L);
  336.  
  337.                 if(broker_mp = (struct MsgPort *)CreatePort(0,0))
  338.                 {
  339.                     newbroker.nb_Port = (struct MsgPort *)broker_mp;
  340.  
  341.                     if (broker = (CxObj *)CxBroker(&newbroker, NULL))
  342.                     {
  343.                         cxsigflag = 1L << broker_mp->mp_SigBit;
  344.                         ActivateCxObj(broker, 1L);
  345.  
  346.                         RetinaBase=(struct Library *)OpenLibrary("retina.library",0L);
  347.  
  348.                         switch (set->Executive)
  349.                         {
  350.                             case    idle_none:
  351.                                     data->executive=idle_none;
  352.                                     break;
  353.                             case    idle_executive:
  354.                                     if(SysInfoBase = (struct Library *)OpenLibrary(SYSINFONAME, SYSINFOVERSION))
  355.                                     {
  356.                                             if(data->si =(struct SysInfo *)InitSysInfo())
  357.                                                 data->executive=idle_executive;
  358.                                             else
  359.                                                 data->executive=idle_none;
  360.                                     }
  361.                                     else data->executive=idle_none;
  362.                                     break;
  363.                             case    idle_own:
  364.                                     init_idle();
  365.                                     data->executive=idle_own;
  366.                                     break;
  367.                         }
  368.  
  369.                         data->scrnot=FALSE; if(set->start_usescreennotify)
  370.                             if(ScreenNotifyBase=(struct Library *)OpenLibrary("screennotify.library",SCREENNOTIFY_VERSION))
  371.                                 data->scrnot=TRUE;
  372.  
  373.                         /* Go to main handler */
  374.  
  375.                         if(openWindow(set,data))
  376.                         {
  377.                             drawBackground(set,data);
  378.                             allocGadgets(set,data,gclass);
  379.                             set=(struct tm_sys_set *)handler(set,data,broker_mp,broker,cxsigflag,gclass);
  380.                             closeWindow(set,data);
  381.                             removeGadgets(set,data);
  382.  
  383.                         }
  384.                         else show("Could not open window!");
  385.  
  386.                         switch (data->executive)
  387.                         {
  388.                             case    idle_executive:
  389.                                     FreeSysInfo(data->si);
  390.                                     CloseLibrary(SysInfoBase);
  391.                                     break;
  392.  
  393.                             case    idle_own:
  394.                                     free_idle();
  395.                                     break;
  396.  
  397.                         }
  398.  
  399.                         if(data->scrnot)CloseLibrary(ScreenNotifyBase);
  400.                         if(RetinaBase)  CloseLibrary(RetinaBase);
  401.  
  402.                         /* Clear any message before killing the cx */
  403.                         while(msg = (CxMsg *)GetMsg(broker_mp)) ReplyMsg((struct Message *)msg);
  404.                         DeleteCxObj(broker);
  405.                     }
  406.                     else show("CxBroker failed.");
  407.                     DeletePort(broker_mp);
  408.  
  409.                     many=data->list;
  410.                     do
  411.                     {
  412.                         act=many->next;
  413.                         FreeVec(many);
  414.                     }
  415.                     while(many=act);
  416.                 }
  417.                 else show("CreatePort failed!");
  418.                 freeGaugeGadgetClass(gclass);
  419.             }
  420.             else show("BOOPSI class allocation failed!");
  421.             FreeVec(set);
  422.         }
  423.         else show("Not enough memory!");
  424.         FreeVec(data);
  425.     }
  426.     else show("No enough memory!");
  427.  
  428. }
  429.